/*
 * @Author: your name
 * @Date: 2021-12-21 10:36:14
 * @LastEditTime: 2021-12-21 18:06:15
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \am\am\am-server\app\service\player.js
 */
const { Service } = require('egg')
const BussinessError = require('../utils/BussinessError')



class PlayerService extends Service {
    // 分页查询用户信息
    async pageQuery ({ page, pageSize }) {
        const { mysql } = this.app;
        
        //1. 查询分页数据
        let sql_select = `select * from match_player limit ${(page - 1) * pageSize},${pageSize}`
        const players = await mysql.query(sql_select)
    
        //2. 统计数量
        const sql = `select count(*) as total from match_player`
        // 获取分页查询的数据条数
        const [{ total }] = await mysql.query(sql)
        // 返回
        return {
          page,
          pageSize,
          total,
          list: players
        }
      }
    // 添加或修改用户信息
    async saveOrUpdate (row) {
      const { mysql } = this.app
      try {
        if (row.id) {
          // 更新 {id:1,name:"terry",age:21}  update match_player set name='terry',no=21 where id = 1
          await mysql.update('match_player', row)
        } else {
          await mysql.insert('match_player', row)
        }
      } catch (error) {
        console.log(error);
        throw new BussinessError('操作失败')
      }
    }
  
}
module.exports = PlayerService
